//
//  DMMyOrderLogisticsVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/18.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMMyOrderLogisticsVC.h"
#import "DMMyOrderLogisticsHeadCell.h"
#import "DMMyOrderLogisticsCell.h"


//model
#import "KLogisticsModel.h"

//request
#import "KStoreRequest.h"
//other
#import "NSDate+Util.h"


@interface DMMyOrderLogisticsVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic,strong)KLogisticsModel * model;

@end

@implementation DMMyOrderLogisticsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    self.navigationItem.title = @"我的订单";
    self.view.backgroundColor = DMWhiteColor;
    _dataArray = [NSMutableArray new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMMyOrderLogisticsCell" bundle:nil] forCellReuseIdentifier:@"DMMyOrderLogisticsCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DMMyOrderLogisticsHeadCell" bundle:nil] forCellReuseIdentifier:@"DMMyOrderLogisticsHeadCell"];
    [self getData];
}


#pragma mark ----netWork
-(void)getData{
    
    KStoreRequest * request = [[KStoreRequest alloc]initLogisticsDetailWithSoEntityId:self.soEntityId];
    
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        KLogisticsModel * logisticsTotalModel = [KLogisticsModel modelWithJSON:baseModel.data];
        
        weakSelf.model = logisticsTotalModel;
        KLogisticsDetailArrayModel * arrModel =[KLogisticsDetailArrayModel modelWithJSON:logisticsTotalModel.expressData];
        weakSelf.model.expressModel = arrModel;
        weakSelf.dataArray = [NSMutableArray arrayWithArray:weakSelf.model.expressModel.data];
        [weakSelf.tableView reloadData];
        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 148;
    }
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    return self.tableView.rowHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DMMyOrderLogisticsHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMMyOrderLogisticsHeadCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"DMMyOrderLogisticsHeadCell" owner:self options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.nameLb.text = [NSString stringWithFormat:@"承运来源:%@",self.model.shipBillView.biShipName];
        
        //物流状态
        NSString * statusStr;
        
        if ([self.model.expressModel.state isEqualToString:@"0"]) {
            statusStr = @"在路上";
        }else if ([self.model.expressModel.state isEqualToString:@"1"]) {
            statusStr = @"已揽件";
        }else if ([self.model.expressModel.state isEqualToString:@"2"]) {
            statusStr = @"货物寄送过程出了问题";
        }else if ([self.model.expressModel.state isEqualToString:@"3"]) {
            statusStr = @"收件人已签收";
        }else if ([self.model.expressModel.state isEqualToString:@"4"]) {
            statusStr = @"退签";
        }else if ([self.model.expressModel.state isEqualToString:@"5"]) {
            statusStr = @"派件中";
        }else if ([self.model.expressModel.state isEqualToString:@"6"]) {
            statusStr = @"退回中";
        }
        
        cell.statusLb.text = statusStr;
        cell.numLb.text = [NSString stringWithFormat:@"运单编号: %@",self.model.shipBillView.biTrackingNo];
//        cell.phoneLb.text = self.model.pho
        return cell;

    }    
    DMMyOrderLogisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMMyOrderLogisticsCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMMyOrderLogisticsCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.lineView.hidden = NO;
    if (indexPath.row == 0) {
        cell.dateLb.font = DMFontSize(14);
        cell.dateLb.textColor = DMFontColor;
        cell.timeLb.textColor = DMFontColor;
        cell.contentLb.textColor = DMFontColor;
        cell.pointImage.image = [UIImage imageNamed:@"yuan_shuangceng"];
    }else {
        cell.dateLb.font = DMFontSize(10);
        cell.dateLb.textColor = RGBCOLOR(171, 168, 168);
        cell.timeLb.textColor = RGBCOLOR(171, 168, 168);
        cell.contentLb.textColor = RGBCOLOR(171, 168, 168);
        cell.pointImage.image = [UIImage imageNamed:@"yuan"];
        if (indexPath.row == _dataArray.count-1) {
            cell.lineView.hidden = YES;
        }
    }
    
    KLogisticsDetailModel * detailModel = self.dataArray[indexPath.row];
    
    
    
    NSRange Range1 = [detailModel.time rangeOfString:@"-"];
    
    NSString * dateStr = [detailModel.time substringWithRange:NSMakeRange(Range1.length+Range1.location, 5)];
    cell.dateLb.text = dateStr;
    
    
    NSRange Range2 = [detailModel.time rangeOfString:@" "];
    NSString * timeStr = [detailModel.time substringWithRange:NSMakeRange(Range2.length+Range2.location, 5)];
    cell.timeLb.text = timeStr;
    cell.contentLb.text = detailModel.context;
    return cell;
}

@end
